package net.chathu.airgw 
{
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author ze
	 */
	public class SoulManager 
	{
		private static var _instance:SoulManager;
		//private var souls:Vector.<Soul>;
		private var souls:Dictionary;
		
		public function SoulManager() 
		{
			if (_instance)
			{
				throw new Error("SoulManager is a Singleton!");
			} else {
				_instance = this;
			}
			//souls = new Vector.<Soul>();
			souls = new Dictionary();

		}
		
		private static function get instance():SoulManager
		{
			if (!_instance)
			{
				new SoulManager();
			}
			
			return _instance;
		}
		
		public static function createSoul():Soul {
			var soul:Soul = new Soul();
			//instance.souls.push(soul);
			instance.souls[soul.id] = soul;
			Debug.echo("Client added: " + soul.id);
			Debug.echo("Total clients: " + total);
			return soul;
		}
		
		public static function removeSoul(soul:Soul):void {
			if (instance.souls[soul.id])
			{
				Debug.echo("Removing client (id: " + soul.id + ")");
				//FONTOS: eloszor legyen a delete,utana a destroy!
				delete instance.souls[soul.id];
				soul.destroy();
			} else {
				//No op
			}
		}
		
		public static function get total():int {
			var num:int=0;
			for (var x:* in instance.souls)
			{
				num++;
			}
			
			return num;
		}
		
		public static function getSoul(id:int):Soul {
			return instance.souls[id];
		}
		
		public static function destroy():void {
			Debug.echo("SoulManager destroy!");
			for (var id:* in instance.souls)
			{
				removeSoul(getSoul(id));
			}
		}
		
	}

}